package me.zhengjie.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.WcsReport.WcsReportService;
import me.zhengjie.pojo.dto.*;
import me.zhengjie.pojo.entity.*;
import me.zhengjie.pojo.vo.ReturnVo;
import me.zhengjie.pojo.vo.TestVo;
import me.zhengjie.service.WmsService;
import me.zhengjie.sqlservice.*;
import me.zhengjie.tcp.FluentFrame;
import me.zhengjie.utility.IdWorker;
import me.zhengjie.utility.state.MaterialStatus;
import me.zhengjie.utility.state.TaskStatus;
import me.zhengjie.utility.state.TaskType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class WmsServiceImpl implements WmsService {
    private static final Logger wms = LoggerFactory.getLogger("wms");

    private final WcsFluentFrameConfigService wcsFluentFrameConfigService;

//    private final FluentFrameTCPA fluentFrameTCPA;
//
//    private final FluentFrameTCPB fluentFrameTCPB;
    private final FluentFrame fluentFrame;

    private final WcsFluentFrameOutboundDetailsService wcsFluentFrameOutboundDetailsService;

    private final WcsFluentFrameOutboundDetailsHistoryService wcsFluentFrameOutboundDetailsHistoryService;

    private final NestingMiddleTableService nestingMiddleTableService;

    private final WcsReportService wcsReportService;

    private final WcsInboundMissionService wcsInboundMissionService;

    private final IdWorker idWorker;

    @Override
    public ReturnVo sortingResult(SortingResultDto sortingResult) {
        wms.info("2楼分拣完成后上报WCS请求参数：{}",sortingResult);
        Integer type = sortingResult.getType();
        String containerCode = sortingResult.getContainerCode();
        WcsFluentFrameOutboundDetails wcsFluentFrameOutboundDetails = wcsFluentFrameOutboundDetailsService.selectContainerCode(containerCode);
        if (wcsFluentFrameOutboundDetails !=null){
            String storageCode = wcsFluentFrameOutboundDetails.getStorageCode();
            Integer id = wcsFluentFrameOutboundDetails.getId();
            WcsFluentFrameConfig wcsFluentFrameConfig = wcsFluentFrameConfigService.selectStorageCode(storageCode);
            if (wcsFluentFrameConfig !=null){
                Integer cylinderControlAddress = wcsFluentFrameConfig.getCylinderControlAddress();
                Integer unitId = wcsFluentFrameConfig.getUnitId();
                Integer size = wcsFluentFrameConfig.getSize();
                Integer wcsFluentFrameConfigId = wcsFluentFrameConfig.getId();
                String region = wcsFluentFrameConfig.getRegion();
                //修改流利架数量
                //修改琉璃架出库状态
                if (type == 1){
                    //告知PLC整箱出库
                    wms.info("发生PLC整箱出库：地址位{}",cylinderControlAddress);
                    boolean b = fluentFrame.writeWithRetry(cylinderControlAddress, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus(), region);
                    if (!b){
                        wms.info("整箱出库写入PLC失败");
                        return ReturnVo.ng("写入PLC失败");
                    }
                    //整箱出库移到历史表
                    WcsFluentFrameOutboundDetailsHistory wcsFluentFrameOutboundDetailsHistory = new WcsFluentFrameOutboundDetailsHistory();
                    BeanUtils.copyProperties(wcsFluentFrameOutboundDetails, wcsFluentFrameOutboundDetailsHistory);
                    wcsFluentFrameOutboundDetailsHistoryService.insertFluentFrameOutboundDetailsHistory(wcsFluentFrameOutboundDetailsHistory);
                    wcsFluentFrameOutboundDetailsService.deleteId(id);
                }else if (type == 2){
                    //告知PLC分解回库
                    wms.info("告知PLC分解回库：地址位{}",cylinderControlAddress);
                    boolean b = fluentFrame.writeWithRetry(cylinderControlAddress, 2, unitId, MaterialStatus.RETRY_COUNT.getStatus(), region);
                    if (!b){
                        wms.info("分解回库写入PLC失败");
                        return ReturnVo.ng("写入PLC失败");
                    }
                    wcsFluentFrameOutboundDetailsService.updateIsNest(id,type);
                }
                if (size !=0){
                    wms.info("修改流利架数量：id：{}：数量{}",id,size);
                    wcsFluentFrameConfigService.updateSize(wcsFluentFrameConfigId,--size);
                }
                return ReturnVo.OK();
            }
        }
        return ReturnVo.ng("容器编码不存在");
    }

    @Override
    public ReturnVo plywood(PlywoodDto plywoodDto) {
        wms.info("流利架合板WMS请求参数:{}",plywoodDto);
        String workstation = plywoodDto.getWorkstation();
        Integer mode = plywoodDto.getMode();
        boolean b = wcsFluentFrameConfigService.updateWorkstationMode(workstation,mode);
        if (!b){
            return ReturnVo.ng("修改合板模式失败");
        }
        return ReturnVo.OK();
    }

    @Override
    public ReturnVo queryNesting(QueryNestingDto queryNestingDto) {
        String containerCode = queryNestingDto.getContainerCode();
        if (containerCode.isEmpty()){
            return ReturnVo.ng("容器编码为空");
        }
        List<NestingMiddleTable> nestingMiddleTables = nestingMiddleTableService.selectContainerCode(containerCode);
        if (!nestingMiddleTables.isEmpty()){
            return ReturnVo.OK(nestingMiddleTables);
        }
        return ReturnVo.ng("容器编码：" + containerCode + "未找到嵌套信息");
    }
}
